import {  logout, getInfo } from '@/api/user'
import { loginPro } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { asyncRouterMap, constantRouterMap } from '@/router'
import router, { resetRouter } from '@/router'

const state = {
  token: '',
  name: '',
  avatar: '',
  roles: '',
  userInfo: {}
  // routes: constantRouterMap,
  // addRoutes: []
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles;
  },
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  },
  SET_ROUTERS:(state, routers) => {
    state.addRoutes = routers
    state.routes = constantRoutes.concat(routers)
  },
  SET_USERINFO:( state, userInfo )=>{
    state.userInfo = userInfo
  }
}

const getters = {
  token: (state) => {
    return state.token
  },
  name: (state) => {
    return state.name
  },
  avatar: (state) => {
    return state.avatar
  },
  roles: (state) => {
    return state.roles
  }
}

const actions = {
  // 设置token
  setToken({ commit }, token){
      commit('SET_TOKEN', token)

  },
  // 存储用户信息
  getUserInfo({ commit }, userInfo){
    commit('SET_USERINFO',userInfo)
    // const role = userInfo.role? userInfo.role: '2'
    // console.log(role)
    // commit('SET_ROLES', role)
    // localStorage.setItem('userInfo',JSON.stringify(userInfo))
    // localStorage.setItem('role',role)
    // console.log( localStorage.getItem('role'))
  },
  // user login 第一个login是$store中的方法，第二个login方法是，api里的login方法，用来调用接口的  调用login接口拿到token
  login({ commit }, userInfo) {
    console.log('login')
    const { username, password } = userInfo

    return new Promise((resolve, reject) => {
      const  paramsData = { phone: username, password: password } 
      console.log(JSON.stringify(paramsData))
      loginPro(paramsData).then(response => {
        let userInfo = JSON.stringify(response.data)
        localStorage.setItem('userInfo', userInfo)
        setToken('55555')
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      // getInfo(state.token).then(response => {
        // console.log(response);
        // const { data } = response

        // if (!data) {
          // reject('Verification failed, please Login again.')
        // }
        // const { name, avatar, roles, asyncRouterMap} = data
         let userInfo = localStorage.getItem('userInfo')
        // 遍历后台传来的路由字符串，转换为组件对象
        //filterAsyncRouter(routers)
        // localStorage.setItem('routerCache',JSON.stringify(asyncRouterMap))
        
        //const menus = routers
        // commit('SET_NAME', name)
        // commit('SET_AVATAR', avatar)
        // commit('SET_ROLES', roles)
        //dispatch('SET_ROUTERS', asyncRouterMap)

        resolve(userInfo)
      })
      //   reject(error)
      // })
  },
  
 
  // user logout
  logout({ commit, state }) {
    // return new Promise((resolve, reject) => {
      // logout(state.token).then(() => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', '')
        localStorage.removeItem("userInfo");
        removeToken()
        // resetRouter()//重置路由
        // resolve('ok')
      // }).catch(error => {
      //   reject(error)
      // })
    // })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()//清除cookies中的token
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}